﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BL.Entity;
using System.Transactions;
using DL;
namespace BL.Service
{
    public class tbl_PayService : ServiceBase
    {
        public List<Pay> GetPay()
        {
            List<Pay> result = new List<Pay>();
            var query = from item in this.Context.tbl_Pay
                        select new Pay()
                        {
                            CreateBy = item.CreateBy,
                            CreateDate = item.CreateDate.Value,
                            Discount = item.Discount.Value,
                            LimitBuy = item.LimitBuy.Value,
                            NumType = item.NumType,
                            PayPrice = item.Pay.Value,
                            Buy=item.Buy.Value,
                            MinStart=item.MinStart.Value,
                            MinEnd=item.MinEnd.Value,
                            UpdateBy = item.UpdateBy,
                            UpdateDate = item.UpdateDate.Value,
                            UserID = item.UserID
                        };
            if (query.ToList() != null)
            {
                result = query.ToList();
            }
            return result;
        }
        public Pay GetLimit(string numberType)
        {
            
            List<Pay> result = new List<Pay>();
            var query = from item in this.Context.tbl_Pay
                        where item.NumType==numberType
                        select new Pay()
                        {
                            CreateBy = item.CreateBy,
                            CreateDate = item.CreateDate.Value,
                            Discount = item.Discount.Value,
                            LimitBuy = item.LimitBuy.Value,
                            NumType = item.NumType,
                            PayPrice = item.Pay.Value,
                            MinStart=item.MinStart.Value,
                             MinEnd=item.MinEnd.Value,
                              Buy=item.Buy.Value,
                            UpdateBy = item.UpdateBy,
                            UpdateDate = item.UpdateDate.Value,
                            UserID = item.UserID
                        };
            if (query != null && query.Count()>0)
            {
                result = query.ToList();
                return result[0];
            }
            else {
                return new Pay() {LimitBuy=0,MinEnd=0, MinStart=0 };
            }

        }
        public bool CheckHaveConnfig()
        {

            return GetPay().Count> 0;
        }
        public void SavePay(List<Pay> payList) {
            if (CheckHaveConnfig())
            {
                UpdateConfig(payList);
            }
            else {
                AddPayConfig(payList);
            }
        }
        public void AddPayConfig(List<Pay> payList)
        {
            TransactionOptions tranOpt = new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted };
            using (var trans = new TransactionScope(TransactionScopeOption.Required, tranOpt))
            {
                DateTime dateNow = GetDateGobal();
                foreach (Pay payItem in payList)
                {
                    tbl_Pay pay = new tbl_Pay()
                    {
                        UserID = payItem.UserID,
                        UpdateDate = dateNow,
                        UpdateBy = payItem.UpdateBy,
                        Pay = payItem.PayPrice,
                        NumType = payItem.NumType,
                        LimitBuy = payItem.LimitBuy,
                        Discount = payItem.Discount,
                         Buy=payItem.Buy,
                          MinEnd=payItem.MinEnd,
                           MinStart=payItem.MinStart,
                        CreateDate = dateNow,
                        CreateBy = payItem.CreateBy,

                    };
                    this.Context.AddTotbl_Pay(pay);
                }
                this.Context.SaveChanges();
                trans.Complete();
            }
        }
        public void UpdateConfig(List<Pay> payList) {
            TransactionOptions tranOpt = new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted };
            using (var trans = new TransactionScope(TransactionScopeOption.Required, tranOpt))
            {
                DateTime dateNow = GetDateGobal();
                foreach (Pay payItem in payList)
                {
                    var query = from item in this.Context.tbl_Pay
                                where item.NumType == payItem.NumType
                                select item;

                    tbl_Pay pay = query.FirstOrDefault();
                    pay.Pay = payItem.PayPrice;
                    pay.LimitBuy = payItem.LimitBuy;
                    pay.Buy = payItem.Buy;
                    pay.MinStart = payItem.MinStart;
                    pay.MinEnd = payItem.MinEnd;
                    pay.Discount = payItem.Discount;
                    pay.UpdateBy = payItem.UpdateBy;
                    pay.UpdateDate = dateNow;
                }
                this.Context.SaveChanges();
                trans.Complete();
            }
        }
    }
}
